﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Validation;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WebDauGia.Domain.Abstract;
//using WebDauGia.Domain.Entities;
using WebDauGia.Domain.Models;

namespace WebDauGia.Domain.Concrete
{
    public class EFRepository:WebRepository
    {
        //private EFDbContext context = new EFDbContext();
        private WebDauGiaDBContext context = new WebDauGiaDBContext();
        #region Sản phẩm
        public IQueryable<SanPham> SanPhams {
            get { return context.SanPhams; }
        }
        public void SaveSanPham(SanPham sp)
        {
            try
            {
                if (sp.id == 0)
                {

                    context.SanPhams.Add(sp);
                }
                else
                {
                    SanPham dbEntry = context.SanPhams.Find(sp.id);
                    if (dbEntry != null)
                    {
                        dbEntry.ten = sp.ten;
                        dbEntry.mota = sp.mota;
                        dbEntry.giaban = sp.giaban;
                        dbEntry.giakhoidiem = sp.giakhoidiem;
                        dbEntry.idhangsx = sp.idhangsx;
                        dbEntry.idloaisp = sp.idloaisp;
                        dbEntry.idnguoiban = sp.idnguoiban;
                        dbEntry.kichthuoc = sp.kichthuoc;
                        dbEntry.mausac = sp.mausac;
                        dbEntry.ngaybatdau = sp.ngaybatdau;
                        dbEntry.ngayketthuc = sp.ngayketthuc;
                        dbEntry.tinhtrang = sp.tinhtrang;
                        dbEntry.trongluong = sp.trongluong;
                       // dbEntry.ImageData = sp.ImageData;
                        //dbEntry.ImageMimetype = sp.ImageMimetype;
                        dbEntry.active = sp.active;
                    }
                }
                context.SaveChanges();
            }
            catch (DbEntityValidationException dbEx)
            {
                Exception raise = dbEx;
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        string message = string.Format("{0}:{1}",
                            validationErrors.Entry.Entity.ToString(),
                            validationError.ErrorMessage);
                        // raise a new exception nesting  
                        // the current instance as InnerException  
                        raise = new InvalidOperationException(message, raise);
                    }
                }
                throw raise; 
            }
        }
        public SanPham DeleteSanPham(int id)
        {
            SanPham dbEntry = context.SanPhams.Find(id);
            if (dbEntry != null)
            {
                context.SanPhams.Remove(dbEntry);
                context.SaveChanges();
            }
            return dbEntry;
        }
        #endregion
        #region Loại Sản Phẩm
        public IQueryable<LoaiSanPham> LoaiSanPhams {
            get { return context.LoaiSanPhams; }
        }
        public void SaveLoaiSP(LoaiSanPham loaisp)
        {
            if (loaisp.id == 0)
            {
                //context.DanhMucSanPhams.Add(dmsp);
                context.LoaiSanPhams.Add(loaisp);
            }
            else
            {
                LoaiSanPham dbEntry = context.LoaiSanPhams.Find(loaisp.id);
                if (dbEntry != null)
                {
                    dbEntry.ten = loaisp.ten;
                    dbEntry.mota = loaisp.mota;
                    dbEntry.iddanhmuc = loaisp.iddanhmuc;
                    dbEntry.active = loaisp.active;
                }
            }
            context.SaveChanges();
        }
        public LoaiSanPham DeleteLoaiSP(int id)
        {
            LoaiSanPham dbEntry = context.LoaiSanPhams.Find(id);
            if (dbEntry != null)
            {
                context.LoaiSanPhams.Remove(dbEntry);
                context.SaveChanges();
            }
            return dbEntry;
        }
        #endregion
        #region Danh Mục Sản Phẩm
        public IQueryable<DanhMucSanPham> DanhMucSanPhams {
            get { return context.DanhMucSanPhams; }
        }
        public void SaveDMSP(DanhMucSanPham dmsp) {
            if (dmsp.id == 0)
            {
                context.DanhMucSanPhams.Add(dmsp);

            }
            else {
                DanhMucSanPham dbEntry = context.DanhMucSanPhams.Find(dmsp.id);
                if (dbEntry != null) {
                    dbEntry.ten = dmsp.ten;
                    dbEntry.mota = dmsp.mota;
                    dbEntry.active = dmsp.active;
                }
            }
            context.SaveChanges();
        }
        public DanhMucSanPham DeleteDMSP(int id) {
            DanhMucSanPham dbEntry = context.DanhMucSanPhams.Find(id);
            if (dbEntry != null) {
                context.DanhMucSanPhams.Remove(dbEntry);
                context.SaveChanges();
            }
            return dbEntry;
        }
        #endregion
        public IQueryable<NhanVien> NhanViens {
            get { return context.NhanViens; }
        }
        //public IQueryable<KhachHang> KhacHangs {
        //    get { return context.KhachHangs; }
       // }
    }
}
